package com.ruoyi.web.controller.pull;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Pull.LineStock;
import com.ruoyi.system.domain.Pull.MaterialFeed;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ILineStockService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 线边库存列Controller
 *
 * @author ruoyi
 * @date 2023-03-06
 */
@Api(value = "LineStockController", tags = "线边库存列接口api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pull/stock" )
public class LineStockController extends BaseController {

    private final ILineStockService iLineStockService;

    /**
     * 查询线边库存列列表
     */
    @ApiOperation(value = "查询线边库存列列表",response = LineStock.class)
    @GetMapping("/list")
    public TableDataInfo list(LineStock lineStock)
    {
        startPage();
        LambdaQueryWrapper<LineStock> lqw = new LambdaQueryWrapper<LineStock>();
        if (StringUtils.isNotBlank(lineStock.getMaterialCode())){
            lqw.eq(LineStock::getMaterialCode ,lineStock.getMaterialCode());
        }
        if (StringUtils.isNotBlank(lineStock.getLineName())){
            lqw.like(LineStock::getLineName ,lineStock.getLineName());
        }
        if (StringUtils.isNotBlank(lineStock.getStationName())){
            lqw.like(LineStock::getStationName ,lineStock.getStationName());
        }
        List<LineStock> list = iLineStockService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出线边库存列列表
     */
    @Log(title = "线边库存列" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(LineStock lineStock) {
        LambdaQueryWrapper<LineStock> lqw = new LambdaQueryWrapper<LineStock>(lineStock);
        List<LineStock> list = iLineStockService.list(lqw);
        ExcelUtil<LineStock> util = new ExcelUtil<LineStock>(LineStock. class);
        return util.exportExcel(list, "stock" );
    }

    /**
     * 获取线边库存列详细信息
     */
    @ApiOperation(value = "获取线边库存列详细信息")
    @GetMapping(value = "/{stockId}" )
    public AjaxResult getInfo(@PathVariable("stockId" ) Long stockId) {
        return AjaxResult.success(iLineStockService.getById(stockId));
    }

    /**
     * 新增线边库存列
     */
    @ApiOperation(value = "新增线边库存列")
    @Log(title = "线边库存列" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LineStock lineStock) {
        lineStock.setCreateBy(SecurityUtils.getUsername());
        lineStock.setCreateTime(new Date());
        return toAjax(iLineStockService.save(lineStock) ? 1 : 0);
    }

    /**
     * 修改线边库存列
     */
    @ApiOperation(value = "修改线边库存列")
    @Log(title = "线边库存列" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LineStock lineStock) {
        lineStock.setUpdateBy(SecurityUtils.getUsername());
        lineStock.setUpdateTime(new Date());
        return toAjax(iLineStockService.updateById(lineStock) ? 1 : 0);
    }

    /**
     * 删除线边库存列
     */
    @ApiOperation(value = "删除线边库存列")
    @Log(title = "线边库存列" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{stockIds}" )
    public AjaxResult remove(@PathVariable Long[] stockIds) {
        return toAjax(iLineStockService.removeByIds(Arrays.asList(stockIds)) ? 1 : 0);
    }
}
